package chess

import (
	"fmt"

	"gddgame.cc/galaxy/asteroid/cluster"
	"gddgame.cc/galaxy/binocle"
	"gddgame.cc/galaxy/common/feature/orbit"
	"gddgame.cc/galaxy/utils/def"
)

type SingleInstance struct {
	*BlockInstance
	*ClientComponent
}

func (layer *SingleInstance) NeedForward() bool {
	return false
}

type SingleService struct {
	def.Logger
	binocle.Service

	Block *BlockService
}

func (service *SingleService) ID() string {
	return "chessSingle"
}
func (service *SingleService) Master(node cluster.Node) {

}
func (service *SingleService) Init(node cluster.Node) {
	node.SetConfig(cluster.ThreadSafe, true)

	token := node.Token()
	mapId := token
	block := orbit.LocationCM.NodeBlock("")

	//fmt.Println("init", mapId, block, levelIndex, level, blockIndexWithLevel[levelIndex+1:], utils.ToInt16(blockIndexWithLevel[levelIndex+1:]))
	blockInstance := &BlockInstance{
		block:    block,
		Datas:    map[byte]*DataManager{},
		Location: block.ToNode(),
	}
	clientComponent := &ClientComponent{
		Node:       node,
		Block:      block,
		MapId:      mapId,
		levelPoint: orbit.Point(0),
		level:      uint(0),
		service:    service,
	}
	instance := &SingleInstance{
		BlockInstance:   blockInstance,
		ClientComponent: clientComponent,
	}
	node.SetInstance(instance)
	service.Block.InitService(node, instance.BlockInstance, instance)
}

func (service *SingleService) InitClient(client *cluster.Client) {
	service.Block.InitClient(client)
}

func (service *SingleService) CloseInstance(node cluster.Node) {

}

func (service *SingleService) CloseClient(client *cluster.Client) {

}
func (service *SingleService) GetWithSub(container cluster.Container, mapId string, sub string, level orbit.Point) *cluster.Client {
	module := service.ID()
	//if strings.Index(sub, ":") > 0 {
	//	debug.PrintStack()
	//}
	token := mapId
	//fmt.Println("sub", sub, level, token)
	if container == nil {
		fmt.Println("block container empty")
	} else if container.Balance() == nil {
		fmt.Println("block balance empty")
	}
	return container.Balance().GenerateClient(module, token)
}

func (service *SingleService) GetSub(size orbit.Location, position []byte, log bool) string {
	return ""
}
